/*
 * Copyright (C) 2008 xyzse
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file XInvertIndex.h
 * @version 0.0.0
 * @author zouxin ( zouxin2008@gmail.com )
 * @date 18/3/2008 0.0.0 created, by zouxin
 */
#ifndef _XINVERTINDEX_H_
#define _XINVERTINDEX_H_

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
#include <vector>

using namespace std;


typedef struct{
	int docid;
	int num;
}WordItem;

//every doc includes several TempItem
typedef struct {
	string word;
	int num;//the count of word in one doc
}TempItem;

//only used for sort WordItem in set 
struct itemcomp
{
	bool operator()(const WordItem &item1,const WordItem &item2)
	{
		if(item1.docid<item2.docid)
			return true;
		else if(item1.docid>item2.docid)
			return false;
		return false;
	}
};

class XInvertIndex
{
	public:
		XInvertIndex(string inputfile,string outputfile);
		~XInvertIndex();
		int beginIndex();		
	private:
		ifstream m_input;
		ofstream m_output;

		map<string,set<WordItem,itemcomp> > invertMap;

	    void insertIndex(int docid,vector<TempItem> &wordvec);

		vector<TempItem> getWords(string &words,int num);



};
#endif
